# coding=gbk

from DataAccess.DBConnFactory import DBConnFactory
from DataAccess.blp_client import query_blp_data
from Error.ExtendError import *
from Misc.Constant import *

import os
from string import Template

		
def format_sql_criteria(column_name, including_values):
	if (not including_values) or (len(including_values)==0):
		return "1=1"
	#elif len(including_values)==1:
	elif isinstance(including_values,str):
		return column_name + "='" + including_values + "'"
	else:
		ret = column_name + ' in ('
		is_first_elem = True
		for elem in including_values:
			if is_first_elem:
				ret += "'" + elem + "'"
				is_first_elem = False
			else:
				ret += ",'" + elem + "'"
		ret += ')'
		return ret
		

def obsolete_set_security_name(ticker, sec_type, sec_name):
	if sec_type=='REPO':
		sec_name = ticker
	elif sec_type=='FUTURES':
		sec_name = ticker
	elif sec_type=='PNOTE':
		cursor.execute("select underlying_ticker from pnote_info where isin_code=' " + ticker + "'")
		tmp=cursor.fetchone()
		if len(tmp) > 0:
			underly_ticker = tmp[0]  
			ticker = underly_ticker #underlying ticker to display
						
		else:
			raise RuntimeError(), ("Unknown pnote, " + ticker)		
			
	elif sec_type=='STOCK':
		pass		
		
	else:
		sec_name = ticker
	
	return

	
extract_dom_cur_from_blp_ticker = lambda t: t[3:6]
extract_for_cur_from_blp_ticker = lambda t: t[0:3]
convert_cur_pair_to_blp_ticker = lambda c1,c2: c2+c1+' curncy'
	
	
def ch_ticker_to_blp_ticker(ticker):
	return ticker + ' CH Equity'
	
def blp_ticker_to_ch_ticker(blp_ticker):
	return blp_ticker[0:6]
	
def hk_ticker_to_blp_ticker(ticker):	
	return str(int(ticker))+' HK Equity'
	
def blp_ticker_to_hk_ticker(blp_ticker):
	num_part = blp_ticker[0:-10]
	return '0'*(5-len(num_part))+num_part
	
def blp_ticker_is_hk_ticker(blp_ticker):
	if blp_ticker.find('HK')>=0:
		return True
	else:
		return False

			

			
	